//Copyright (C) 2009 Ron Coleman. Contact: ronncoleman@gmail.com
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 3 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

package org.workplicity.marist.grounds.util;

import org.workplicity.marist.grounds.GroundsRequest;
import org.workplicity.marist.grounds.ui.table.RequestTableModel;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import org.workplicity.entry.User;

/**
 *
 * @author Ron Coleman
 */
public class CustomRenderer extends DefaultTableCellRenderer {
    private Color bgChangeColor = new Color(255,128,64);
    private Color fgChangeColor = Color.black;

    private Color bgSelectedColor = Color.yellow;
    private Color fgSelectedColor = new Color(255,255,255);

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        
//        User user = Helper.whoAmI(GroundsWorklet.getInstance().getContext());

        RequestTableModel model = (RequestTableModel) table.getModel();

        User user = model.getUser();

        CompositeOrder co = model.getRow(row);

        GroundsRequest request = co.getRequest();

        if(isSelected) {
            setBackground(bgSelectedColor);
            setForeground(fgSelectedColor);
        }
        else if(user.getStamp() == null || user.getStamp().getTime() < request.getUpdateDate().getTime()) {
            setBackground(bgChangeColor);
            setForeground(fgChangeColor);
        }
        else {
            setBackground(Color.white);
            setForeground(Color.black);
        }

        // If the priority is urgent, change it's color
        if(column == 3 && request.getPriority() == GroundsRequest.Priority.URGENT) {
            setBackground(Color.green);
            setForeground(Color.black);
        }
        setForeground(Color.black);

        return this;
    }
}
